//
//  ZWProductsModel.m
//  aixianfeng
//
//  Created by zhaowei on 17/5/5.
//  Copyright © 2017年 zhaowei. All rights reserved.
//

#import "ZWProductsModel.h"
#import "ZWMarketLeftTableViewModel.h"
#import "ZWMarketManager.h"

@implementation ZWProductsModel

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setup];
    }
    return self;
}


#pragma mark - 设置 -
- (void)setup {
    
    //请求数据
    NSDictionary *dict = [[ZWMarketManager sharedInstance] gainMarketInfomation];
    
    
    //创建模型
    ZWMarketLeftTableViewModel *model = [ZWMarketLeftTableViewModel new];
    
    //取出id数组
    NSArray *idArr = model.idArr;
    
    //创建可变数组
    NSMutableArray *nameArr = [NSMutableArray array];
    NSMutableArray *priceArr = [NSMutableArray array];
    NSMutableArray *imgUrlArr = [NSMutableArray array];
    
    
    //遍历idArr
    for (NSNumber *indexID in idArr) {
        NSString *index = [NSString stringWithFormat:@"%@",indexID];
        
        NSDictionary *productsDict = dict[@"data"][@"products"];
        NSArray *productsArr = [productsDict valueForKey:index];
        //遍历字典的Key
    
        NSInteger count = productsArr.count;
        
        //创建数组
        NSMutableArray *nameMutable = [NSMutableArray array];
        NSMutableArray *priceMutable = [NSMutableArray array];
        NSMutableArray *imgUrlMutable = [NSMutableArray array];
        //循环添加
        for (NSInteger i =0; i<count; i++) {
            NSString *name =  productsArr[i][@"name"];
            NSString *price = productsArr[i][@"price"];
            NSString *imgUrl = productsArr[i][@"img"];
            //添加到数组
            [nameMutable addObject:name];
            [priceMutable addObject:price];
            [imgUrlMutable addObject:imgUrl];
        }
        
        //添加到可变数组
        [nameArr addObject:nameMutable];
        [priceArr addObject:priceMutable];
        [imgUrlArr addObject:imgUrlMutable];
    }
    
    //给属性赋值
    self.nameArr = nameArr;
    self.priceArr = priceArr;
    self.imgUrlArr = imgUrlArr;
    
}

@end
